export default {
	data() {
		return {
			//数据
			uploadNum: 0,
			validate: {}, //验证对象
			wrkDynamic: {}, //工作动态信息
      tempName: '',
			defaultUrl:require("../../images/ExpertsShow/bg-img.png"),//默认图片
			showMsg:false,//图片提示是否显示
			fatherIndex: 0, //一级菜单index
		}
	},
	created() { //调用方法
		let that = this;

	},
	mounted() {
		let that = this;
		UE.delEditor('editor');
		var ue = UE.getEditor('editor', {
			maximumWords: 10000
		});
    ue.addListener( 'contentChange', function( editor ) {
      $("textarea[name='content']").val(ue.getContent());
      $("textarea[name='content']").valid();
    });
		UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
		UE.Editor.prototype.getActionUrl = function(action) {
			if(action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {
				return that.urls.uploadUediter;
			} else if(action == 'uploadvideo') { //视频
				return that.urls.uploadUediter;
			} else if(action == 'uploadfile') { //附件
				return that.urls.uploadUediter;
			} else {
				return this._bkGetActionUrl.call(this, action);
			}
		};
		let uuid = that.get("byTheNewsid");
		if(uuid != null && uuid != "") {
			that.$post(
					that.urls.getWorkDynamic, {
						"uuid": uuid
					}
				)
				.then(function(res) {
					if(res.success) {
						that.wrkDynamic = res.data;
						if (res.data != null) {
              that.tempName = res.data.name;
            }
						ue.ready(function() {
							ue.setContent(that.wrkDynamic.content);
						});
            $("#enclosure").html(that.getFileName(that.wrkDynamic.enclosureUrl));
					}
				})
		}

		//校验名称重复
		$.validator.addMethod("checkNameRepeat", function(value, element, params) {
			let flg = false;
			if(that.wrkDynamic.id != null && (value == that.tempName)) {
				flg = true;
			} else {
				$.ajax({
					type: "POST",
					async: false,
					url: that.urls.checkWorkDynamicName,
					data: JSON.stringify({
						"name": value
					}),
					dataType: "json",
					contentType: "application/json",
					success: function(data) {
						if(data.success) {
							flg = !data.data;
						}
					}
				});
			}
			return this.optional(element) || flg;
		}, "该标题已存在！");

    //校验editor内容
    /*$.validator.addMethod("checkEditorContent", function(value, element, params) {
      let flg = false;
      let count = ue.getContentLength(true);
      if (count > 0 && count <= 10000) {
        flg = true;
      }
      return this.optional(element) || flg;
    }, "动态内容最多只能输入10000个字!");*/

		//加载表单验证
		that.validate = $("#newsReleaseForm").validate({
			ignore: [],
			rules: {
				name: { //动态名称
					required: true,
					maxlength: 50,
					checkNameRepeat: true
				},
				coverUrl: { // 动态封面图片
					required: true,
					maxlength: 500
				},
        enclosureUrl: { // 附件
          maxlength: 500
        },
				content: { // 动态内容
					required: true
				}
			},
			messages: {
				name: { // 动态名称
					required: "请输入标题",
					maxlength: "标题最多只能输入50个字!"
				},
				coverUrl: { // 动态封面图片
					required: "请选择展示图片",
					maxlength: "展示图片最多只能输入500个字!"
				},
        enclosureUrl: { // 附件
          maxlength: "展示图片最多只能输入500个字!"
        },
				content: { // 动态内容
					required: "请输入动态内容",
				}
			},
//			errorElement: "em",
//			errorPlacement: function(error, element) {
//				error.appendTo(element.parents(".error-box").children(".error-msg"));
//			}
		});
	},
	updated:function(){
		if(this.getParamValue("fatherIndex")){
			this.fatherIndex=this.getParamValue("fatherIndex");
		}
		if(this.getParamValue("childIndex")){
			this.childIndex=this.getParamValue("childIndex");
		}
	},
	methods: { //写函数方法
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			} else if(n == 2) {
        $("#handIdFront2").click();
      }
		},
    //删除附件
    deleteEnclosure: function() {
      $("#enclosure").html("");
      $("input[name='enclosureUrl']").val("");
    },
    //上传文件
		idFrontImg: function(event) {
			var that = this;
			var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
			let formData = new FormData();
			formData.append('fileNames', file);
			let config = {
				headers: {
					'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
				}
			};
			that.$post(
					that.urls.upload,
					formData,
					config
				)
				.then(function(res) {
					if(res.success) {
						let fileInfo = res.data[0];
						$(".showHandIdFront").attr("src", that.urls.baseUrl + fileInfo.url);
						that.wrkDynamic.coverUrl=fileInfo.url;
						that.showMsg=false;
						$(".handIdFrontA").css("display", "none");
						$("input[name='coverUrl']").val(fileInfo.url);
					}
				})
		},
    //上传文件
    idFrontImg2: function(event) {
      var that = this;
      var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
      let formData = new FormData();
      formData.append('fileNames', file);
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
        }
      };
      that.$post(
        that.urls.upload,
        formData,
        config
      )
        .then(function(res) {
          if(res.success) {
            let fileInfo = res.data[0];
            $("#enclosure").html(that.getFileName(fileInfo.url));
            $("input[name='enclosureUrl']").val(fileInfo.url);
          }
        })
    },
		//保存
		saveData: function() {
			let that = this;
			let flg = that.validate.form();
			$("#coverUrl-error").css("display","none");
			that.showMsg=true;
			if(flg) {
				let dataObj = that.combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.addWorkDynamic,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							that.wrkDynamic = res.data;
							msg = "发布成功！";
							that.pressRelease();
						} else {
							msg = "发布失败！";
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			}
		},
		//修改
		updateData: function() {
			let that = this;
			let flg = that.validate.form();
			if(flg) {
				let dataObj = that.combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.updateWorkDynamic,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							that.wrkDynamic = res.data;
							msg = "保存成功！";
							that.pressRelease();
						} else {
							msg = "保存失败！";
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			}
		},
		//返回工作动态列表页
		pressRelease: function() {
			var that = this;
			that.$router.push({
				path: "PressRelease?fatherIndex="+this.fatherIndex,
			})
		},
	}
}
